package com.example.a2205douyin.db;

import android.app.Application;

import androidx.room.Room;

/**
 * 数据库的工具类
 */
public class AppDBUtils {
    //单例恶汉
    private AppDBUtils(){}
    private static AppDBUtils appDBUtils = new AppDBUtils();
    public static AppDBUtils getInstance()
    {
        return appDBUtils;
    }
    private AppDataBase appDataBase;
    private VideoDao videoDao;
    private AccountDao accountDao;

    public void init(Application application){
        appDataBase = Room.databaseBuilder(application,AppDataBase.class,"2205A.db")
                .allowMainThreadQueries()//允许在主线程中访问
                .build();
        videoDao = appDataBase.getVideoDao();
        accountDao = appDataBase.getAccountDao();
    }
    //防止空指针异常  在内存不足的情况下，videoDao可能会被回收
    public VideoDao  getVideoDao(){
        if(videoDao==null){
            videoDao = appDataBase.getVideoDao();
        }
        return videoDao;
    }
    public AccountDao getAccountDao(){
        if(accountDao==null){
            accountDao = appDataBase.getAccountDao();
        }
        return accountDao;
    }
}
